<?php


namespace app\admin\controller;


use app\common\controller\Common;
use app\index\controller\Base;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Banner extends Base
{


    //幻灯片列表
    public function index()
    {
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);
        $where = [];
        $keyword = $community_id = '';
        //获取小区
        $community_list[0] = ['id' => -1, 'propertyName' => "平台幻灯片"];
        $this->assign("community_list", $community_list);
        if (!empty($_GET['keyword'])) {
            $where['name'] = ['like', "%" . $_GET['keyword'] . "%"];
            $keyword = $_GET['keyword'];
        }
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("admin_banner")->order("id desc")->where($where)->limit($start,$limit)->select();
                //字段拼接
                foreach ($list as $k => $v) {
                    $list[$k]['propertyName'] = "平台广告" ;
                    $v['path'] = empty($v['path']) ? "/static/images/nopic-107.png" : $v['path'];
                    $list[$k]['path'] = "<img src='".$v['path']."' style='height:50px'>";
                }
                $count = Db::name("admin_banner")->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        $assign = [
            "keyword" => $keyword,
            "community_id" => intval($community_id)
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }


    //添加视图
    public function addBanner()
    {
        $community[0] = ['id' => 0, 'propertyName' => "平台幻灯片"];
        $this->assign("community", $community);

        //启用状态
        $enable_status = Db::name("config_attribute")->where(['key' => "enable_status"])->value("value");
        $enable_status = explode("|", $enable_status);
        foreach ($enable_status as $k => $v) {
            $enable_status[$k + 1] = $v;
        }
        unset($enable_status[0]);
        $this->assign("enable_status", $enable_status);
        if (empty($_GET['id'])) {
            return $this->fetch('create');
        } else {
            //获取基本信息
            $detail = Db::name("admin_banner")->where(['id' => $_GET['id']])->find();
            $this->assign("detail", $detail);
            return $this->fetch('update');
        }
    }

    //执行添加操作
    public function doBanner()
    {
        try {
            $common = new Common();
            $rule = [
                ["name", "require", "请填写广告名称"],
                ["path", "require", "请上传广告文件"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            //查询小区名称
            $propertyName = "平台";
            if (empty($data['id'])) {
                $data['addtime'] = time();
                $data['add_id'] = Session::get("id");
                $id = Db::name("admin_banner")->insertGetId($data);
                $log['remark'] = "【" . Session::get("user_name") . "】添加新的广告【" . $data['name'] . "】,id【" . $id . "】,所属小区【" . $propertyName . "】";
            } else {
                $data['updatetime'] = time();
                $data['update_id'] = Session::get("id");
                $id = $data['id'];
                Db::name("admin_banner")->update($data);
                $log['remark'] = "【" . Session::get("user_name") . "】修改广告【" . $data['name'] . "】,id【" . $id . "】,所属小区【" . $propertyName . "】";
            }
            $log['controller'] = "Banner";
            $log['methods'] = "doBanner";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    public function delBanner()
    {
        try {
            $id = $_POST['id'];
            $list = Db::name("admin_banner")->where(['id' => ['in', $id]])->select();
            $res = Db::name("admin_banner")->where(['id' => ['in', $id]])->delete();
            if (empty($res)) {
                throw new Exception("删除失败");
            }
            $common = new Common();
            foreach ($list as $k => $v) {
                $log['remark'] = "【" . Session::get("user_name") . "】删除幻灯片【" . $v['name'] . "】";
                $log['controller'] = "Banner";
                $log['methods'] = "delBanner";
            }
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


}